package sctu.edu.java.year2016.team06.chenqiang.day20160610.chapter15_3;



/**
 * Created by XQ on 2016/6/10.
 * 尝试开发一个窗体，在窗体中设置一个进度条，使进度条每次递增滚动。
 */
import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.awt.Dimension;

import java.awt.event.ActionEvent;


import java.awt.event.ActionListener;

import java.util.logging.Level;

import java.util.logging.Logger;


import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JProgressBar;

import javax.swing.Timer;

import javax.swing.UIManager;

import javax.swing.event.ChangeEvent;

import javax.swing.event.ChangeListener;
public class ProgressBar implements ActionListener, ChangeListener{
    JFrame frame = null;
    JProgressBar progressbar;
    JLabel label;
    Timer timer;
    JButton b;

    public ProgressBar() {
        frame = new JFrame("进度条");
        frame.setBounds(100, 100, 400, 130);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        Container contentpanel = frame.getContentPane();
        label = new JLabel("点击运行按钮开始", JLabel.CENTER);
        progressbar = new JProgressBar();
        progressbar.setOrientation(JProgressBar.HORIZONTAL);
        progressbar.setMinimum(0);
        progressbar.setMaximum(100);
        progressbar.setValue(0);
        progressbar.setStringPainted(true);
        progressbar.addChangeListener(this);

        progressbar.setPreferredSize(new Dimension(300, 20));

        progressbar.setBorderPainted(true);

        progressbar.setBackground(Color.pink);
        JPanel panel = new JPanel();
        b = new JButton("运行");

        b.setForeground(Color.blue);

        b.addActionListener(this);

        panel.add(b);

        timer = new Timer(100, this);


        contentpanel.add(panel, BorderLayout.NORTH);

        contentpanel.add(label, BorderLayout.CENTER);

        contentpanel.add(progressbar, BorderLayout.SOUTH);


        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == b) {

            timer.start();

        }
        if (e.getSource() == timer) {

            int value = progressbar.getValue();

            if (value < 100)

                progressbar.setValue(++value);

            else {
                timer.stop();

                frame.dispose();

            }

        }
    }

    public void stateChanged(ChangeEvent e1) {

        int value = progressbar.getValue();

        if (e1.getSource() == progressbar) {

            label.setText("目前已完成进度：" + Integer.toString(value) + "%");

            label.setForeground(Color.blue);
        }
    }

    public static void main(String[] args) {

        try {

            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {

            Logger.getLogger(ProgressBar.class.getName()).log(Level.FINE,

                    e.getMessage());

            e.printStackTrace();

        }

        new ProgressBar();

    }
}

